package com.neuedu.service.impl;

import com.neuedu.dao.CommentDao;
import com.neuedu.dao.ProductDao;
import com.neuedu.model.Comment;
import com.neuedu.model.Product;
import com.neuedu.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class ProductServiceImpl implements ProductService {
    @Autowired
    private  ProductDao productDao;

    @Autowired
    private CommentDao commentDao;





    @Override
    public Product queryProductById(Integer pid) {
        Product product = productDao.selectById(pid);
        return product;
    }




    //商品信息管理-添加
    @Override
    public void addProduct(Product product) {
        productDao.save(product);
    }
    // 减少库存
    @Override
    public boolean reduceStock(Integer pid, Integer quantity) {
        int rows = productDao.reduceStock(pid, quantity);
        return rows > 0;
    }

    // 查询商品库存
    @Override
    public Integer getStockById(Integer pid) {
        return 100;
    }

    // 添加商品评价
    @Override
    public void addComment(Comment comment) {
        commentDao.insert(comment);
    }

    // 查询商品的所有评价
    @Override
    public List<Comment> getCommentsByProductId(Integer pid) {
        Comment comment = new Comment();
        comment.setPid(pid);
        comment.setContent("zzzzz");
        List<Comment> comments = new ArrayList<>();
        comments.add(comment);
        return comments;
//        return commentDao.selectCommentsByProductId(pid);
    }

}
